const {
	reqGet,
	reqPost,
} = require('@utils/request')

module.exports = {

	openTemplateDialog(editType) {
		let {
			store,
			dailogComp,
			templateForm,
			tableComp,
			parameterList,
			formName,
			formDesc,
		} = this
		let formDialog = dailogComp.formDialog
		store.editType = editType
		store.isLock = editType == 'add' || editType == 'update'
		store.updateTemplate = null
		templateForm.form('clear')
		parameterList.treegrid('loadData', [])
		if (editType != 'add') {
			let row = tableComp.datagrid('getSelected')
			if (!row) {
				$.messager.alert({
					title: '提示',
					msg: '请选择一个模板！',
				})
				return false
			}
			templateForm.form('load', row)
			store.updateTemplate = row
			let treeData = null
			try {
				let template = row.template.replace(/^["'`]|["'`]$/g, '')
				treeData = JSON.parse(template)
			} catch (e) {
				treeData = []
			}
			parameterList.treegrid('loadData', treeData)
		}
		formName.textbox('readonly', editType == 'detail')
		formDesc.textbox('readonly', editType == 'detail')
		formDialog.dialog('open')
	},

	removeTemplate() {
		let {
			tableComp,
		} = this
		let selected = tableComp.datagrid('getSelected')
		if (!selected || !selected.id) {
			$.messager.alert({
				title: '提示',
				msg: '请选择一个模板！'
			})
			return false
		}
		$.messager.confirm({
			title: '提示',
			msg: '确认删除模板？',
			fn(r) {
				if (r) {
					reqPost('/ult/template/delete', {
						template_id: selected.id,
					}).then(result => {
						tableComp.datagrid('reload')
					})
				}
			}
		})
	}
}